package com.ns.service;

import java.util.List;

import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ns.dao.QuestionDao;
import com.ns.dao.model.VObjQuestion;
import com.ns.vo.QuestionDetailObject;
import com.ns.vo.SubQuestionDetailObject;

@Service
@Transactional
public class QuestionService {
	@Autowired
	private QuestionDao dao;

	public List<VObjQuestion> findAll() {
		return dao.findAll();
	}

	public List<QuestionDetailObject> findByPaper(Integer paperid) {
		String sql = "SELECT pr.question_id AS qid, q.type_id AS type, p. NAME AS pointPath,pr.pastpaper_part_id as part,pr.display_order as disOrder," +
				" q.stem AS stem, CASE WHEN q.is_multi_part=1 THEN 1 ELSE 0 END AS muilti, q.multi_id AS mutiId,q.source_year as year," +
				" q.choice_1 AS choice1, q.choice_2 AS choice2, q.choice_3 AS choice3, q.choice_4 AS choice4, " +
				"q.comment_author AS author, q.stand_answer AS standAnswer, CASE q.difficult_grade WHEN - 2.4 THEN '简单' WHEN - 1.2 THEN '较易' WHEN 0 THEN '中等' WHEN 1.2 THEN '较难' WHEN 2.4 THEN '困难' END AS difficult, " +
				"q.point AS point, q.answer_comment AS answerComment, q.recovery_error AS error, q.answer_skill AS skill, q.answer_expand AS expand, q.source AS source, q.fb1z2 AS fb1z2, q.fb1z3 AS fb1z3 FROM v_pastpaper_question_r pr " +
				"LEFT JOIN v_obj_question q ON pr.question_id = q.PUKEY LEFT JOIN v_question_pk_r t ON t.question_id = pr.question_id AND t.bb102 !=-1 " +
				"LEFT JOIN v_knowledge_point p ON t.pk_id = p.PUKEY " +
				"WHERE pr.pastpaper_id = ? and pr.bb102=1 GROUP BY pr.question_id ORDER BY pr.display_order ASC";
		return dao.findToTransformerBySql(sql, QuestionDetailObject.class, paperid);
	}
	public List<QuestionDetailObject> findByTestPaper(Integer paperid) {
		String sql = "SELECT pr.question_id AS qid, q.type_id AS type, p. NAME AS pointPath,pr.paper_part_id as part,pr.display_order as disOrder," +
				" q.stem AS stem, CASE WHEN q.is_multi_part=1 THEN 1 ELSE 0 END AS muilti, q.multi_id AS mutiId,q.source_year as year," +
				" q.choice_1 AS choice1, q.choice_2 AS choice2, q.choice_3 AS choice3, q.choice_4 AS choice4, " +
				"q.comment_author AS author, q.stand_answer AS standAnswer, CASE q.difficult_grade WHEN - 2.4 THEN '简单' WHEN - 1.2 THEN '较易' WHEN 0 THEN '中等' WHEN 1.2 THEN '较难' WHEN 2.4 THEN '困难' END AS difficult, " +
				"q.point AS point, q.answer_comment AS answerComment, q.recovery_error AS error, q.answer_skill AS skill, q.answer_expand AS expand, q.source AS source, q.fb1z2 AS fb1z2, q.fb1z3 AS fb1z3 FROM v_testpaper_question_r pr " +
				"LEFT JOIN v_obj_question q ON pr.question_id = q.PUKEY LEFT JOIN v_question_pk_r t ON t.question_id = pr.question_id AND t.bb102 != -1 " +
				"LEFT JOIN v_knowledge_point p ON t.pk_id = p.PUKEY " +
				"WHERE pr.testpaper_id = ? and pr.bb102=1 GROUP BY pr.question_id ORDER BY pr.display_order ASC";
		return dao.findToTransformerBySql(sql, QuestionDetailObject.class, paperid);
	}
	
	public List<SubQuestionDetailObject> findShenlunByPaper(Integer paperid) {
		String sql = "SELECT pr.question_id AS qid, q.type_id AS type, pr.pastpaper_part_id AS part, pr.display_order AS disOrder," +
				" q.stem AS stem, q.multi_id AS mutiId," +
				"q.source_year AS year, q.answer_comment AS answerComment, q.answer_require AS answer_require, " +
				"q.analyze_question AS analyze_question, q.answer_think AS answer_think, q.bestow_point_explain AS bestow_point_explain, " +
				"q.comment_author AS author, concat(q.difficult_grade,'')  AS difficult, q.point AS point, q.source AS source FROM v_pastpaper_question_r pr" +
				" LEFT JOIN v_sub_question q ON pr.question_id = q.PUKEY WHERE pr.pastpaper_id = ? AND pr.bb102 = 1 ORDER BY pr.display_order ASC";
		return dao.findToTransformerBySql(sql, SubQuestionDetailObject.class, paperid);
	}
	public List<SubQuestionDetailObject> findShenlunByTestPaper(Integer paperid) {
		String sql = "SELECT pr.question_id AS qid, q.type_id AS type, pr.paper_part_id AS part, pr.display_order AS disOrder," +
				" q.stem AS stem, q.multi_id AS mutiId," +
				"q.source_year AS year, q.answer_comment AS answerComment, q.answer_require AS answer_require, " +
				"q.analyze_question AS analyze_question, q.answer_think AS answer_think, q.bestow_point_explain AS bestow_point_explain, " +
				"q.comment_author AS author, concat(q.difficult_grade,'')  AS difficult, q.point AS point, q.source AS source FROM v_testpaper_question_r pr" +
				" LEFT JOIN v_sub_question q ON pr.question_id = q.PUKEY WHERE pr.testpaper_id = ? AND pr.bb102 = 1 ORDER BY pr.display_order ASC";
		return dao.findToTransformerBySql(sql, SubQuestionDetailObject.class, paperid);
	}
}
